package com.yx1996.serviceimpl;

import com.yx1996.dao.CustomerDao;
import com.yx1996.daoimpl.CustomerDaoImpl;
import com.yx1996.domain.Customer;
import com.yx1996.service.CustomerService;
import com.yx1996.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.List;

public class CustomerServiceImpl implements CustomerService {


    private CustomerDao customerDao = new CustomerDaoImpl();
    @Override
    public void save(Customer c) {
        Session session = HibernateUtils.getCurrentSession();
        Transaction tx = session.beginTransaction();
        try {
            customerDao.save(c);
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }finally {
            tx.commit();
        }
    }

    @Override
    public List<Customer> getAll() {
        Session session = HibernateUtils.getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Customer> list = customerDao.getAll();
        tx.commit();
        return list;
    }

}
